/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */

package org.jeecg.modules.api.wlhy.gas;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.third.nucarf.GasService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.gas.entity.HyShippingNoteGas;
import org.jeecg.modules.wlhy.gas.service.IHyShippingNoteGasService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;

/**
 * @Description: App货主-油气管理
 * @author: duanlikao
 * @date: 2022年11月14日 10:00
 */
@Api(tags = "App货主-油气管理")
@RestController
@RequestMapping("/api/wlhy/customer/gas")
public class ApiShippingNoteGasController {


    @Autowired
    private IHyShippingNoteGasService hyShippingNoteGasService;

    @Autowired
    private GasService gasService;

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Autowired
    private IHyCustomerService hyCustomerService;


    @ApiOperation(value = "hy_shipping_note_gas-分页列表查询", notes = "hy_shipping_note_gas-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(HyShippingNoteGas hyShippingNoteGas,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {

        HyCustomer hyCustomer  = hyCustomerService.getCustomer(req);

        QueryWrapper<HyShippingNoteGas> queryWrapper = new QueryWrapper();


        queryWrapper.eq("note.customer_id",hyCustomer.getId());

        //油气状态(1待充值2已充值 3待回收 4已回收 5 回收失败 9充值失败)
        //失败列表 5,9  成功列表 2,4,5
        String gasStatus = hyShippingNoteGas.getGasStatus();
        if(StringUtils.isNotEmpty(gasStatus)){
            if(gasStatus.contains(",")) {
                queryWrapper.in("gas.gas_status", Arrays.asList(gasStatus.split(",")));
            }else {
                queryWrapper.eq("gas.gas_status", gasStatus);
            }
        }


        if(StringUtils.isNotEmpty(hyShippingNoteGas.getShippingNoteNumber())){
            queryWrapper.like("gas.shipping_note_number", hyShippingNoteGas.getShippingNoteNumber());
        }
        if(StringUtils.isNotEmpty(hyShippingNoteGas.getDriverName())){
            queryWrapper.like("gas.driver_name", hyShippingNoteGas.getDriverName());
        }

        if(StringUtils.isNotEmpty(hyShippingNoteGas.getVehicleNumber())){
            queryWrapper.like("gas.vehicle_number", hyShippingNoteGas.getVehicleNumber());
        }

        if(StringUtils.isNotEmpty(hyShippingNoteGas.getIzUsedGas())){
            queryWrapper.eq("gas.iz_used_gas", hyShippingNoteGas.getIzUsedGas());
        }


        if(StringUtils.isNotEmpty(hyShippingNoteGas.getRechargeTime_begin())){
            queryWrapper.ge("gas.recharge_time",hyShippingNoteGas.getRechargeTime_begin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(hyShippingNoteGas.getRechargeTime_end() )){
            queryWrapper.le("gas.recharge_time",hyShippingNoteGas.getRechargeTime_end()+" 23:59:59" );
        }

        queryWrapper.orderByDesc("gas.create_time");

        Page<HyShippingNoteGas> page = new Page<>(pageNo, pageSize);
        IPage<HyShippingNoteGas> pageList = hyShippingNoteGasService.getPageList(page, queryWrapper);

        return Result.OK(pageList);
    }


    @AutoLog(value = "油气充值失败，重新充值")
    @ApiOperation(value = "油气充值失败，重新充值", notes = "油气充值失败，重新充值")
    @PutMapping(value = "/recharge")
    public Result<?> recharge(@RequestParam(name = "id", required = true) String id,HttpServletRequest req) {
        HyCustomer hyCustomer  = hyCustomerService.getCustomer(req);

        this.checkInfo( id , hyCustomer.getId() );

        return gasService.rechargeFail(id);
    }

    @AutoLog(value = "油气充值回收，重新充值")
    @ApiOperation(value = "油气充值回收，重新充值", notes = "油气充值回收，重新充值")
    @PutMapping(value = "/rechargeBack")
    public Result<?> rechargeBack(@RequestBody JSONObject jsonObject,HttpServletRequest req) {

        HyCustomer hyCustomer  = hyCustomerService.getCustomer(req);


        String id = jsonObject.getString("id");
        String money = jsonObject.getString("money");
        String vehicleEnergyType = jsonObject.getString("vehicleEnergyType");

        this.checkInfo( id , hyCustomer.getId() );

        if(StringUtils.isBlank(money)){
            return Result.error("充值金额不能为空!");
        }
        BigDecimal moneyBigDecimal = new BigDecimal(money);
        if(moneyBigDecimal.compareTo(BigDecimal.ZERO) == 0){
            return Result.error("充值金额不能为空!");
        }

        if(StringUtils.isBlank(vehicleEnergyType)){
            return Result.error("车辆能源类型不为空!");
        }
        String as = "E,B";
        int result1 = as.indexOf(vehicleEnergyType);
        if (result1 == -1) {
            return Result.error("车辆能源类型不正确! 车辆能源类型为天然气、柴油其他类型暂不支持");
        }

        return gasService.rechargeBack( id , money  , vehicleEnergyType);
    }


    @AutoLog(value = "油气回收")
    @ApiOperation(value = "油气回收", notes = "油气回收")
    @PutMapping(value = "/back")
    public Result<?> back(@RequestParam(name = "id", required = true) String id ,HttpServletRequest req) {

        HyCustomer hyCustomer  = hyCustomerService.getCustomer(req);

        this.checkInfo( id , hyCustomer.getId() );

        return gasService.back(id);
    }

   private void checkInfo(String id ,String customerId ){

       if(StringUtils.isBlank(id)){
           throw new JeecgBootException("油气充值信息不能为空!");
       }

       if(StringUtils.isBlank(customerId)){
           throw new JeecgBootException("货主信息不能为空!");
       }

        HyShippingNoteGas shippingNoteGas =  hyShippingNoteGasService.getById(id);
        if(shippingNoteGas == null ){
            throw new JeecgBootException("油气充值信息不能为空!");
        }

        HyShippingNote hyShippingNote = hyShippingNoteService.getById(shippingNoteGas.getShippingId());
        if(hyShippingNote == null ){
            throw new JeecgBootException("运单信息不能为空!");
        }
        if(!hyShippingNote.getCustomerId().equals(customerId)){
            throw new JeecgBootException("运单货主信息不一致!");
        }
    }



}
